//
//  JWSearchStuClockCtryVC.m
//  test
//
//  Created by 朱圳 on 2018/3/21.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "JWSearchStuClockCtryVC.h"
#import "HasClockcell.h"
#import "StuClockHisVC.h"
@interface JWSearchStuClockCtryVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *contentArry;

@end

@implementation JWSearchStuClockCtryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"搜索结果";
    self.xlTableView.dataSource = self;
    self.xlTableView.delegate = self;
    self.xlTableView.backgroundColor = [UIColor whiteColor];
    self.xlTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self SearchStudent];
    [self setBack];
}
-(void)setBack{
    /*设置导航栏上面的内容*/
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted];
    // 设置尺寸
    backBtn.size = backBtn.currentBackgroundImage.size;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
    /**
     解决自定义返回 手势失效
     */
    self.navigationController.interactivePopGestureRecognizer.delegate=(id)self;
}
/**
 搜索学生
 */
-(void)SearchStudent{
    [self.contentArry removeAllObjects];
    NSDictionary *dic=@{
                        @"jwid":JWid,
                        @"jwpassword":JWpassword,
                        @"searchname":self.searchText,
                        };
    [[AFNetworkTool shareManager]postJSONWithUrl: JWSDsearch_list parmas:dic successData:^(id json) {
        //  NSLog(@"--json--%@", json);
        //学生数据
        __weak typeof(self) weakslf = self;
        NSArray *GRarry=json[@"studentlist"];
        for (int i=0; i<GRarry.count; i++){
            SDUserModel *model = [SDUserModel yy_modelWithJSON:GRarry[i]];
            [weakslf.contentArry addObject:model ];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.xlTableView reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    } ];
}


#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArry.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"cell";
    HasClockcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //根据
    if (cell == nil) {
        
        cell = [[HasClockcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    cell.model = self.contentArry[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StuClockHisVC *vc = [[StuClockHisVC alloc]init];
    SDUserModel *model = self.contentArry[indexPath.row];
    vc.model = model;
    
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    if ([model.revisetime isEqualToString:@"0"]) {
        vc.days = @"0";
    }else{
        double days = ([timeString doubleValue] - [model.revisetime doubleValue])/86400 +1;
        vc.days = [NSString stringWithFormat:@"%.f",days];
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}


-(NSMutableArray *)contentArry{
    if(_contentArry==nil){
        _contentArry=[[NSMutableArray alloc]init];
    }
    return _contentArry;
}
@end
